{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/\n",
      "Collecting langserve[all]\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/cf/1adf5ad4457faf8e4a45c6f40bbc9cba0dfa51dff56ef42293f7d3721912/langserve-0.2.3-py3-none-any.whl (1.2 MB)\n",
      "     ---------------------------------------- 0.0/1.2 MB ? eta -:--:--\n",
      "     -- ------------------------------------- 0.1/1.2 MB 2.0 MB/s eta 0:00:01\n",
      "     ----- ---------------------------------- 0.2/1.2 MB 2.1 MB/s eta 0:00:01\n",
      "     -------- ------------------------------- 0.3/1.2 MB 2.3 MB/s eta 0:00:01\n",
      "     ------------ --------------------------- 0.4/1.2 MB 2.5 MB/s eta 0:00:01\n",
      "     ---------------- ----------------------- 0.5/1.2 MB 2.5 MB/s eta 0:00:01\n",
      "     ------------------- -------------------- 0.6/1.2 MB 2.6 MB/s eta 0:00:01\n",
      "     ----------------------- ---------------- 0.7/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     --------------------------- ------------ 0.8/1.2 MB 2.7 MB/s eta 0:00:01\n",
      "     ------------------------------- -------- 0.9/1.2 MB 2.7 MB/s eta 0:00:01\n",
      "     --------------------------------- ------ 1.0/1.2 MB 2.7 MB/s eta 0:00:01\n",
      "     ------------------------------------- -- 1.1/1.2 MB 2.7 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------  1.2/1.2 MB 2.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------- 1.2/1.2 MB 1.3 MB/s eta 0:00:00\n",
      "Requirement already satisfied: httpx>=0.23.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langserve[all]) (0.27.0)\n",
      "Requirement already satisfied: langchain-core<0.3,>=0.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langserve[all]) (0.2.38)\n",
      "Requirement already satisfied: orjson>=2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langserve[all]) (3.10.6)\n",
      "Requirement already satisfied: pydantic>=1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langserve[all]) (2.8.2)\n",
      "Collecting pyproject-toml<0.0.11,>=0.0.10 (from langserve[all])\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3b/56/95d700e725946200ec9b2aeee4479fcf3ca24cf6fbf0aa548160980787a5/pyproject_toml-0.0.10-py3-none-any.whl (6.9 kB)\n",
      "Requirement already satisfied: fastapi<1,>=0.90.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langserve[all]) (0.111.1)\n",
      "Collecting sse-starlette<2.0.0,>=1.3.0 (from langserve[all])\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/34/7e/d6087916bf58a4343459b47807a116a3a755e6ddd4857f375547e00f6252/sse_starlette-1.8.2-py3-none-any.whl (8.9 kB)\n",
      "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (0.37.2)\n",
      "Requirement already satisfied: typing-extensions>=4.8.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (4.12.2)\n",
      "Requirement already satisfied: fastapi-cli>=0.0.2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (0.0.4)\n",
      "Requirement already satisfied: jinja2>=2.11.2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (3.1.4)\n",
      "Requirement already satisfied: python-multipart>=0.0.7 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (0.0.9)\n",
      "Requirement already satisfied: email_validator>=2.0.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi<1,>=0.90.1->langserve[all]) (2.2.0)\n",
      "Requirement already satisfied: uvicorn>=0.12.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (0.30.3)\n",
      "Requirement already satisfied: anyio in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpx>=0.23.0->langserve[all]) (4.4.0)\n",
      "Requirement already satisfied: certifi in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpx>=0.23.0->langserve[all]) (2024.7.4)\n",
      "Requirement already satisfied: httpcore==1.* in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpx>=0.23.0->langserve[all]) (1.0.5)\n",
      "Requirement already satisfied: idna in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpx>=0.23.0->langserve[all]) (3.7)\n",
      "Requirement already satisfied: sniffio in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpx>=0.23.0->langserve[all]) (1.3.1)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from httpcore==1.*->httpx>=0.23.0->langserve[all]) (0.14.0)\n",
      "Requirement already satisfied: PyYAML>=5.3 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langchain-core<0.3,>=0.1->langserve[all]) (6.0.1)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langchain-core<0.3,>=0.1->langserve[all]) (1.33)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langchain-core<0.3,>=0.1->langserve[all]) (0.1.116)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langchain-core<0.3,>=0.1->langserve[all]) (24.1)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langchain-core<0.3,>=0.1->langserve[all]) (8.5.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from pydantic>=1->langserve[all]) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from pydantic>=1->langserve[all]) (2.20.1)\n",
      "Requirement already satisfied: setuptools>=42 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (70.3.0)\n",
      "Requirement already satisfied: wheel in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.43.0)\n",
      "Collecting toml (from pyproject-toml<0.0.11,>=0.0.10->langserve[all])\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl (16 kB)\n",
      "Requirement already satisfied: jsonschema in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (4.23.0)\n",
      "Requirement already satisfied: dnspython>=2.0.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from email_validator>=2.0.0->fastapi<1,>=0.90.1->langserve[all]) (2.6.1)\n",
      "Requirement already satisfied: typer>=0.12.3 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (0.12.3)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jinja2>=2.11.2->fastapi<1,>=0.90.1->langserve[all]) (2.1.5)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3,>=0.1->langserve[all]) (3.0.0)\n",
      "Requirement already satisfied: requests<3,>=2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (2.32.3)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from anyio->httpx>=0.23.0->langserve[all]) (1.2.2)\n",
      "Requirement already satisfied: click>=7.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn>=0.12.0->uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (8.1.7)\n",
      "Requirement already satisfied: colorama>=0.4 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (0.4.6)\n",
      "Requirement already satisfied: httptools>=0.5.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (0.6.1)\n",
      "Requirement already satisfied: python-dotenv>=0.13 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (1.0.1)\n",
      "Requirement already satisfied: watchfiles>=0.13 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (0.22.0)\n",
      "Requirement already satisfied: websockets>=10.4 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (11.0.3)\n",
      "Requirement already satisfied: attrs>=22.2.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (23.2.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (2023.12.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.35.1)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.19.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (3.3.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (2.2.2)\n",
      "Requirement already satisfied: shellingham>=1.3.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (1.5.4)\n",
      "Requirement already satisfied: rich>=10.11.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (13.7.1)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (3.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (2.18.0)\n",
      "Requirement already satisfied: mdurl~=0.1 in g:\\anaconda3\\envs\\chatocr\\lib\\site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (0.1.2)\n",
      "Installing collected packages: toml, pyproject-toml, langserve, sse-starlette\n",
      "Successfully installed langserve-0.2.3 pyproject-toml-0.0.10 sse-starlette-1.8.2 toml-0.10.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# %pip install langchain-community\n",
    "# %pip install langchain\n",
    "# %pip install openai\n",
    "# %pip install -qU langchain-openai\n",
    "%pip install \"langserve[all]\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from http import HTTPStatus\n",
    "import dashscope\n",
    "\n",
    "\n",
    "def simple_multimodal_conversation_call():\n",
    "    \"\"\"Simple single round multimodal conversation call.\n",
    "    \"\"\"\n",
    "    messages = [\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
    "                {\"image\": \"https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg\"},\n",
    "                {\"text\": \"这是什么?\"}\n",
    "            ]\n",
    "        }\n",
    "    ]\n",
    "    response = dashscope.MultiModalConversation.call(model='qwen-vl-plus',\n",
    "                                                     messages=messages)\n",
    "    # The response status_code is HTTPStatus.OK indicate success,\n",
    "    # otherwise indicate request is failed, you can get error code\n",
    "    # and message from code and message.\n",
    "    if response.status_code == HTTPStatus.OK:\n",
    "        print(response)\n",
    "    else:\n",
    "        print(response.code)  # The error code.\n",
    "        print(response.message)  # The error message.\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    simple_multimodal_conversation_call()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "BadRequestError",
     "evalue": "Error code: 400 - {'error': {'code': None, 'param': None, 'message': \"null is not one of ['system', 'assistant', 'user', 'tool', 'function'] - 'messages.['0].role'\", 'type': 'invalid_request_error'}}",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mBadRequestError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[10], line 28\u001b[0m\n\u001b[0;32m     24\u001b[0m     \u001b[38;5;28mprint\u001b[39m(completion\u001b[38;5;241m.\u001b[39mmodel_dump_json())\n\u001b[0;32m     27\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 28\u001b[0m     \u001b[43mget_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[10], line 11\u001b[0m, in \u001b[0;36mget_response\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_response\u001b[39m():\n\u001b[0;32m      6\u001b[0m     client \u001b[38;5;241m=\u001b[39m OpenAI(\n\u001b[0;32m      7\u001b[0m         api_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msk-6b88a3d68c4a43d8a00bbb8f048f7610\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m      8\u001b[0m         \u001b[38;5;66;03m# api_key=os.getenv(\"DASHSCOPE_API_KEY\"), # 如果您没有配置环境变量，请在此处用您的API Key进行替换\u001b[39;00m\n\u001b[0;32m      9\u001b[0m         base_url\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://dashscope.aliyuncs.com/compatible-mode/v1\u001b[39m\u001b[38;5;124m\"\u001b[39m,  \u001b[38;5;66;03m# 填写DashScope服务的base_url\u001b[39;00m\n\u001b[0;32m     10\u001b[0m     )\n\u001b[1;32m---> 11\u001b[0m     completion \u001b[38;5;241m=\u001b[39m \u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     12\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mqwen-vl-plus\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     13\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[0;32m     14\u001b[0m \u001b[43m            \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrole\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msystem\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mYou are a helpful assistant.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     15\u001b[0m \u001b[43m            \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtype\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtext\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtext\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m这是什么\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     16\u001b[0m \u001b[43m            \u001b[49m\u001b[43m{\u001b[49m\n\u001b[0;32m     17\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtype\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mimage_url\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     18\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mimage_url\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[0;32m     19\u001b[0m \u001b[43m                    \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43murl\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhttps://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n\u001b[0;32m     20\u001b[0m \u001b[43m                \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     21\u001b[0m \u001b[43m            \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     22\u001b[0m \u001b[43m        \u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     23\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     24\u001b[0m     \u001b[38;5;28mprint\u001b[39m(completion\u001b[38;5;241m.\u001b[39mmodel_dump_json())\n",
      "File \u001b[1;32mg:\\anaconda3\\envs\\chatOCR\\lib\\site-packages\\openai\\_utils\\_utils.py:277\u001b[0m, in \u001b[0;36mrequired_args.<locals>.inner.<locals>.wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    275\u001b[0m             msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMissing required argument: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquote(missing[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    276\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg)\n\u001b[1;32m--> 277\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mg:\\anaconda3\\envs\\chatOCR\\lib\\site-packages\\openai\\resources\\chat\\completions.py:646\u001b[0m, in \u001b[0;36mCompletions.create\u001b[1;34m(self, messages, model, frequency_penalty, function_call, functions, logit_bias, logprobs, max_tokens, n, parallel_tool_calls, presence_penalty, response_format, seed, service_tier, stop, stream, stream_options, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[0;32m    612\u001b[0m \u001b[38;5;129m@required_args\u001b[39m([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m], [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstream\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[0;32m    613\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcreate\u001b[39m(\n\u001b[0;32m    614\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    644\u001b[0m     timeout: \u001b[38;5;28mfloat\u001b[39m \u001b[38;5;241m|\u001b[39m httpx\u001b[38;5;241m.\u001b[39mTimeout \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m|\u001b[39m NotGiven \u001b[38;5;241m=\u001b[39m NOT_GIVEN,\n\u001b[0;32m    645\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ChatCompletion \u001b[38;5;241m|\u001b[39m Stream[ChatCompletionChunk]:\n\u001b[1;32m--> 646\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    647\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/chat/completions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    648\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    649\u001b[0m \u001b[43m            \u001b[49m\u001b[43m{\u001b[49m\n\u001b[0;32m    650\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmessages\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    651\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodel\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    652\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfrequency_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrequency_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    653\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunction_call\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    654\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunctions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunctions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    655\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogit_bias\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogit_bias\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    656\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    657\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax_tokens\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    658\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mn\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    659\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mparallel_tool_calls\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_tool_calls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    660\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpresence_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpresence_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    661\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mresponse_format\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    662\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mseed\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mseed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    663\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mservice_tier\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mservice_tier\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    664\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstop\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    665\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    666\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream_options\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    667\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtemperature\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    668\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtool_choice\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtool_choice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    669\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtools\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    670\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_logprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_logprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    671\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_p\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_p\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    672\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43muser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    673\u001b[0m \u001b[43m            \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    674\u001b[0m \u001b[43m            \u001b[49m\u001b[43mcompletion_create_params\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCompletionCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    675\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    676\u001b[0m \u001b[43m        \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    677\u001b[0m \u001b[43m            \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\n\u001b[0;32m    678\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    679\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mChatCompletion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    680\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    681\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mStream\u001b[49m\u001b[43m[\u001b[49m\u001b[43mChatCompletionChunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    682\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mg:\\anaconda3\\envs\\chatOCR\\lib\\site-packages\\openai\\_base_client.py:1266\u001b[0m, in \u001b[0;36mSyncAPIClient.post\u001b[1;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[0;32m   1252\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpost\u001b[39m(\n\u001b[0;32m   1253\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m   1254\u001b[0m     path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1261\u001b[0m     stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1262\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ResponseT \u001b[38;5;241m|\u001b[39m _StreamT:\n\u001b[0;32m   1263\u001b[0m     opts \u001b[38;5;241m=\u001b[39m FinalRequestOptions\u001b[38;5;241m.\u001b[39mconstruct(\n\u001b[0;32m   1264\u001b[0m         method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpost\u001b[39m\u001b[38;5;124m\"\u001b[39m, url\u001b[38;5;241m=\u001b[39mpath, json_data\u001b[38;5;241m=\u001b[39mbody, files\u001b[38;5;241m=\u001b[39mto_httpx_files(files), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[0;32m   1265\u001b[0m     )\n\u001b[1;32m-> 1266\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n",
      "File \u001b[1;32mg:\\anaconda3\\envs\\chatOCR\\lib\\site-packages\\openai\\_base_client.py:942\u001b[0m, in \u001b[0;36mSyncAPIClient.request\u001b[1;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[0;32m    933\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrequest\u001b[39m(\n\u001b[0;32m    934\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m    935\u001b[0m     cast_to: Type[ResponseT],\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    940\u001b[0m     stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m    941\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ResponseT \u001b[38;5;241m|\u001b[39m _StreamT:\n\u001b[1;32m--> 942\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    943\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    944\u001b[0m \u001b[43m        \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    945\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    946\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    947\u001b[0m \u001b[43m        \u001b[49m\u001b[43mremaining_retries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mremaining_retries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    948\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mg:\\anaconda3\\envs\\chatOCR\\lib\\site-packages\\openai\\_base_client.py:1046\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[1;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[0;32m   1043\u001b[0m         err\u001b[38;5;241m.\u001b[39mresponse\u001b[38;5;241m.\u001b[39mread()\n\u001b[0;32m   1045\u001b[0m     log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRe-raising status error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 1046\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_make_status_error_from_response(err\u001b[38;5;241m.\u001b[39mresponse) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1048\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_response(\n\u001b[0;32m   1049\u001b[0m     cast_to\u001b[38;5;241m=\u001b[39mcast_to,\n\u001b[0;32m   1050\u001b[0m     options\u001b[38;5;241m=\u001b[39moptions,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1053\u001b[0m     stream_cls\u001b[38;5;241m=\u001b[39mstream_cls,\n\u001b[0;32m   1054\u001b[0m )\n",
      "\u001b[1;31mBadRequestError\u001b[0m: Error code: 400 - {'error': {'code': None, 'param': None, 'message': \"null is not one of ['system', 'assistant', 'user', 'tool', 'function'] - 'messages.['0].role'\", 'type': 'invalid_request_error'}}"
     ]
    }
   ],
   "source": [
    "# from openai import OpenAI\n",
    "# import os\n",
    "\n",
    "\n",
    "# def get_response():\n",
    "#     client = OpenAI(\n",
    "#         api_key=\"sk-6b88a3d68c4a43d8a00bbb8f048f7610\",\n",
    "#         # api_key=os.getenv(\"DASHSCOPE_API_KEY\"), # 如果您没有配置环境变量，请在此处用您的API Key进行替换\n",
    "#         base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",  # 填写DashScope服务的base_url\n",
    "#     )\n",
    "#     completion = client.chat.completions.create(\n",
    "#         model=\"qwen-vl-plus\",\n",
    "#         messages=[\n",
    "#             {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "#             {\"type\": \"text\", \"text\": \"这是什么\"},\n",
    "#             {\n",
    "#                 \"type\": \"image_url\",\n",
    "#                 \"image_url\": {\n",
    "#                     \"url\": \"https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg\"\n",
    "#                 },\n",
    "#             },\n",
    "#         ],\n",
    "#     )\n",
    "#     print(completion.model_dump_json())\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "#     get_response()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "好的\n",
      "，\n",
      "现在\n",
      "我需要从图中\n",
      "抽取出文本内容并以JSON的形式\n",
      "返回结果。以下是根据您提供的图像\n",
      "抽取的信息：\n",
      "\n",
      "```json\n",
      "{\n",
      "\n",
      "   \"附件\": [\n",
      "      {\n",
      "         \"\n",
      "标题\": \"华南理工大学家庭经济困难\n",
      "本科生认定申请表\",\n",
      "         \"具体内容\n",
      "\": [\n",
      "            \"院系：物理\n",
      "专业\\n年级：大三\\n班级\n",
      "：3207年\\n学\n",
      "号：90123456\"\n",
      "         ]\n",
      "      },\n",
      "\n",
      "      {\n",
      "         \"表格\": [\n",
      "            {\n",
      "\n",
      "               \"姓名\": \"林浩\",\n",
      "\n",
      "               \"性别\": \"男\",\n",
      "\n",
      "               \"出生日期\": \"198\n",
      "1-05-20\",\n",
      "\n",
      "               \"民族\": \"汉\",\n",
      "\n",
      "               \"身份证号码\": \"430\n",
      "10019810\n",
      "52012345\n",
      "6\",\n",
      "               \"户籍地址\": \"\",\n",
      "\n",
      "               \"联系电话\": \"137\n",
      "01370000\n",
      "\",\n",
      "               \"家庭人均年收入（\n",
      "元）\": \">7000\n",
      "\",\n",
      "               \"家庭人口数\": \"\n",
      "3\",\n",
      "               \"主要家庭成员情况\n",
      "\": [\n",
      "                  {\n",
      "                     \"姓名\":\n",
      " \"父亲 林贵\",\n",
      "                     \"\n",
      "年龄\": \"49\",\n",
      "                     \"\n",
      "与学生关系\": \"父母亲\",\n",
      "\n",
      "                     \"工作学习单位\": \"农民\n",
      "\",\n",
      "                     \"从业情况\": \"打\n",
      "零工\",\n",
      "                     \"文化程度\":\n",
      " \"小学\",\n",
      "                     \"年收入(\n",
      "元)\": \"一般\",\n",
      "                     \"健康\n",
      "状况\": \"\"\n",
      "                  },\n",
      "                  {\n",
      "\n",
      "                     \"姓名\": \"母亲 张宁\n",
      "\",\n",
      "                     \"年龄\": \"47\n",
      "\",\n",
      "                     \"与学生关系\": \"\n",
      "母亲\",\n",
      "                     \"工作学习单位\n",
      "\": \"无业\",\n",
      "                     \"从业\n",
      "情况\": \"家庭主妇\",\n",
      "\n",
      "                     \"文化程度\": \"初中\",\n",
      "\n",
      "                     \"年收入(元)\": \"0\",\n",
      "                     \"健康状况\": \"良好\n",
      "\"\n",
      "                  }\n",
      "               ],\n",
      "               \"影响\n",
      "家庭经济状况有关信息\": [\n",
      "\n",
      "                  {},\n",
      "                  {}\n",
      "               ],\n",
      "               \"个人\n",
      "承诺\": \"[内容略]\"\n",
      "            }\n",
      "\n",
      "         ]\n",
      "      }\n",
      "   ]\n",
      "}\n",
      "``\n",
      "`\n",
      "\n",
      "请注意这个回答是基于当前的\n",
      "视觉输入和我的训练数据生成的\n",
      "，并且可能不包含所有潜在的关键\n",
      "细节或上下文。如果有任何问题\n",
      "，请随时告诉我！\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "import os\n",
    "import base64\n",
    "\n",
    "\n",
    "#  base 64 编码格式\n",
    "def encode_image(image_path):\n",
    "    with open(image_path, \"rb\") as image_file:\n",
    "        return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
    "\n",
    "\n",
    "def get_response(image_path):\n",
    "    base64_image = encode_image(image_path)\n",
    "    client = OpenAI(\n",
    "        api_key=\"sk-6b88a3d68c4a43d8a00bbb8f048f7610\",\n",
    "        base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    )\n",
    "    completion = client.chat.completions.create(\n",
    "        model=\"qwen-vl-plus\",\n",
    "        messages=[\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": [\n",
    "                    {\n",
    "                        \"type\": \"text\",\n",
    "                        \"text\": \"\"\"\n",
    "<指令>\n",
    "    你现在的任务是从图片中提取关键信息。请你直接提取不要询问我。\n",
    "    输出为json格式。让我们逐步思考。\n",
    "<指令>\"\"\",\n",
    "                    },\n",
    "                    {\n",
    "                        \"type\": \"image_url\",\n",
    "                        \"image_url\": {\"url\": f\"data:image/jpeg;base64,{base64_image}\"},\n",
    "                    },\n",
    "                ],\n",
    "            }\n",
    "        ],\n",
    "        stream=True,\n",
    "    )\n",
    "    for chunk in completion:\n",
    "        print(chunk.model_dump()['choices'][0]['delta']['content'])\n",
    "    \n",
    "\n",
    "\n",
    "# if __name__=='__main__':\n",
    "result1 = get_response(\"../image/a83.jpg\")\n",
    "print(result1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\HYZ\\AppData\\Local\\Temp\\ipykernel_20300\\1359115435.py:6: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 1.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n",
      "  llm = ChatOpenAI(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"content\": \"我是来自阿里云的大规模语言模型，我叫通义千问。\", \"additional_kwargs\": {}, \"response_metadata\": {\"token_usage\": {\"completion_tokens\": 17, \"prompt_tokens\": 22, \"total_tokens\": 39}, \"model_name\": \"qwen-vl-plus\", \"system_fingerprint\": null, \"finish_reason\": \"stop\", \"logprobs\": null}, \"type\": \"ai\", \"name\": null, \"id\": \"run-7aba8c27-0c14-493b-85e3-eb6627b6fb96-0\", \"example\": false, \"tool_calls\": [], \"invalid_tool_calls\": [], \"usage_metadata\": null}\n"
     ]
    }
   ],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "import os\n",
    "\n",
    "def get_response():\n",
    "    llm = ChatOpenAI(\n",
    "        api_key=os.getenv(\"DASHSCOPE_API_KEY\"), # 如果您没有配置环境变量，请在此处用您的API Key进行替换\n",
    "        base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\", # 填写DashScope base_url\n",
    "        model=\"qwen-vl-plus\"\n",
    "        )\n",
    "    messages = [\n",
    "        {\"role\":\"system\",\"content\":\"You are a helpful assistant.\"}, \n",
    "        {\"role\":\"user\",\"content\":\"你是谁？\"}\n",
    "    ]\n",
    "    response = llm.invoke(messages)\n",
    "    print(response.json(ensure_ascii=False))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    get_response()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ciao'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "api_key=os.getenv(\"DASHSCOPE_API_KEY\")\n",
    "base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\" # 填写DashScope base_url\n",
    "\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "model = ChatOpenAI(api_key=api_key,\n",
    "                   base_url=base_url,\n",
    "                   model=\"qwen-turbo\")\n",
    "\n",
    "\n",
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "\n",
    "messages = [\n",
    "    SystemMessage(content=\"Translate the following from English into Italian\"),\n",
    "    HumanMessage(content=\"hi!\"),\n",
    "]\n",
    "\n",
    "# result=model.invoke(messages) #输出包括很多信息\n",
    "# # 单独调用输出解析器解析响应\n",
    "\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "parser = StrOutputParser()\n",
    "# parser.invoke(result)\n",
    "\n",
    "\n",
    "# # 模型+解析器链接在一起\n",
    "# chain =  model | parser\n",
    "# chain.invoke(messages)\n",
    "\n",
    "\n",
    "\n",
    "# 创建提示模版\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "system_template = \"Translate the following into {language}:\"\n",
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
    ")\n",
    "\n",
    "result = prompt_template.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
    "\n",
    "result\n",
    "result.to_messages() #   [SystemMessage(content='Translate the following into italian:'), HumanMessage(content='hi')]\n",
    "\n",
    "\n",
    "# 创建链将三个链接在一起\n",
    "chain = prompt_template | model | parser\n",
    "chain.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatOCR",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
